using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.Core.DAL;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsEditModules.OtherNews
{
    public partial class OtherNews : Portal.API.Module
    {
        string newsId = "0";
        string categoryId = "0";
        string numberOfNews = "0";
        protected string lastLink = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            bindData();
        }

        private void bindData()
        {
            LoadRuntimeSettings();

            newsId = Convert.ToString(ReadRuntimeConfig(true, "NewsID"));
            categoryId = Convert.ToString(ReadRuntimeConfig(true, "CategoryID"));
            numberOfNews = Convert.ToString(ReadRuntimeConfig(true, "NumberOfOtherNews"));

            if (categoryId != "" && categoryId != "0")
            {
                if (numberOfNews == "" || numberOfNews == "0")
                    numberOfNews = "5";
                int number = Convert.ToInt16(numberOfNews);
                numberOfNews = Convert.ToString(number + 1); 
                
                DataTable table = getNews();
                if (table.Rows.Count > number)
                {
                    lastLink = Portal.NewsHelper.BuildNewsURL(Convert.ToInt64(table.Rows[number]["News_ID"]));
                    table.Rows.RemoveAt(number);
                }
                rptNews.DataSource = table;
                rptNews.DataBind();
            }
        }

        //private void bindNews(string newsIdStr)
        //{
            //if (newsIdStr != "")
            //{
            //    DataTable newsTable = new DataTable();
            //    newsTable.Columns.Add("News_ID", typeof(Int64));
            //    newsTable.Columns.Add("News_Title", typeof(string));
            //    Hashtable table = getNews(newsIdStr);
            //    string[] newsIds = newsIdStr.Split(',');
            //    foreach (string newsId in newsIds)
            //    {
            //        DataRow row = newsTable.NewRow();
            //        row[0] = newsId;
            //        row[1] = table[newsId];
            //        newsTable.Rows.Add(row);
            //        //rptNews.Items.Add(new ListItem(Convert.ToString(table[newsId]), newsId));
            //    }
            //    rptNews.DataSource = newsTable;
            //    rptNews.DataBind();
            //}
        //}

        //private string formatDimension(string d)
        //{
        //    try
        //    {
        //        Convert.ToInt32(d);
        //        d += "px";
        //    }
        //    catch { }
        //    return d;
        //}

        protected string formatDisplayDateTime(object obj)
        {
            try
            {
                return Convert.ToDateTime(obj).ToString("dd/MM/yyyy") ;
            }
            catch {
                return "";
            }
        }

        protected string buildNewsUrl(object newsId) 
        {
            try
            {
                return Portal.NewsHelper.BuildNewsURL(Convert.ToInt64(newsId));
            }
            catch
            {
                return "#";
            }
        }

        private DataTable getNews()
        {
            using (MainDB db = new MainDB())
            {
                string sql = "";
                if (newsId == "" || newsId == "0")
                    sql = "Select top " + numberOfNews + " News_ID, News_Title, News_PublishDate from News where Cat_ID = " + categoryId + " order by News_PublishDate desc, News_ID desc";
                else
                {
                    sql = " declare @publishDate datetime";
                    sql += " set @publishDate = (Select News_PublishDate from News where News_ID = " + newsId + ")";
                    sql += " Select top " + numberOfNews + " News_ID, News_Title, News_PublishDate ";
					sql += " from News where Cat_ID = " + categoryId + " and News_PublishDate <= @publishDate and News_ID != " + newsId;
                    sql += " order by News_PublishDate desc, News_ID desc"; 
                }
                DataTable dt = db.SelectQuery(sql);
                db.Close();
                return dt;
            }
        }
    }
}